﻿// 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
// (“Fluent UI”)。该示例仅供参考，
// 用以补充《Microsoft 基础类参考》和
// MFC C++ 库软件随附的相关电子文档。
// 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
// 若要了解有关 Fluent UI 许可计划的详细信息，请访问
// https://go.microsoft.com/fwlink/?LinkId=238214.
//
// 版权所有(C) Microsoft Corporation
// 保留所有权利。

// CheckerView.cpp: CheckerView 类的实现
//

#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "zChecker.h"
#endif

#include "MainFrm.h"
#include "CheckerDoc.h"
#include "CheckerView.h"

#include "widgets/zxMetaSchemaDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CheckerView

IMPLEMENT_DYNCREATE(CheckerView, CFormView)

BEGIN_MESSAGE_MAP(CheckerView, CFormView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CheckerView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_COMMAND(ID_BTN_TEST_LANDSAT5_METADATA, &CheckerView::OnBtnTestLandsat5Metadata)
	ON_COMMAND(ID_BTN_TEST_LANDSAT_METADATA_CHECK, &CheckerView::OnBtnTestLandsatMetadataCheck)
	ON_COMMAND(ID_BTN_META_SCHEMA_LANDSAT05, &CheckerView::OnBtnMetaSchemaLandsat05)
	ON_COMMAND(ID_BTN_META_SCHEMA_LANDCOVER30_IMAGE, &CheckerView::OnBtnMetaSchemaLandcover30Image)
	ON_COMMAND(ID_BTN_META_SCHEMA_LANDCOVER30_MAP, &CheckerView::OnBtnMetaSchemaLandcover30Map)
END_MESSAGE_MAP()

// CheckerView 构造/析构

CheckerView::CheckerView() noexcept
	: CFormView(IDD_ZCHECKER_FORM)
{
	// TODO: 在此处添加构造代码

}

CheckerView::~CheckerView()
{
}

void CheckerView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}

BOOL CheckerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CFormView::PreCreateWindow(cs);
}

void CheckerView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	

	//初始化MapControl控件
	CWnd* pWndCal = GetDlgItem(IDC_MAPCONTROL1);
	LPUNKNOWN pUnk = pWndCal->GetControlUnknown();
	HRESULT hr = pUnk->QueryInterface(IID_IMapControl2, (LPVOID *)&m_ipMapControl);
	if (FAILED(hr))
	{
		AfxMessageBox("初始化MapControl失败");
	}


	//CWnd* pWndTOC = GetDlgItem(IDC_TOCCONTROL1);
	//pUnk = pWndTOC->GetControlUnknown();
	//hr = pUnk->QueryInterface(IID_ITOCControl2, (LPVOID *)&m_ipTOCControl);
	//if (FAILED(hr))
	//{
	//	AfxMessageBox("初始化TOCControl失败");
	//}
	//m_ipTOCControl->SetBuddyControl(m_ipMapControl);

	CMainFrame* pFrmMain = (CMainFrame*)AfxGetMainWnd();
	pFrmMain->m_wndTocPane.m_wndTocForm.SetBuddyMapControl(m_ipMapControl);

}


// CheckerView 打印


void CheckerView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

BOOL CheckerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CheckerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CheckerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}

void CheckerView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: 在此处添加自定义打印代码
}

void CheckerView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CheckerView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CheckerView 诊断

#ifdef _DEBUG
void CheckerView::AssertValid() const
{
	CFormView::AssertValid();
}

void CheckerView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CheckerDoc* CheckerView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CheckerDoc)));
	return (CheckerDoc*)m_pDocument;
}
#endif //_DEBUG


// CheckerView 消息处理程序


int CheckerView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	//// 初始化许可
	//::CoInitialize(NULL);
	//ArcGISVersionLib::IArcGISVersionPtr ipVer(__uuidof(ArcGISVersionLib::VersionManager));
	//VARIANT_BOOL succeeded;
	//// 10.2版本
	//if (FAILED(ipVer->LoadVersion(ArcGISVersionLib::esriArcGISEngine, L"10.2", &succeeded)))
	//{	//初始化失败，返回-1
	//	return -1;
	//}

	//IAoInitializePtr m_AoInit;//(CLSID_AoInitialize);
	//m_AoInit.CreateInstance(CLSID_AoInitialize);
	//esriLicenseStatus ls;
	//HRESULT h = m_AoInit->Initialize(esriLicenseProductCode::esriLicenseProductCodeEngineGeoDB, &ls);

	return 0;
}


void CheckerView::OnSize(UINT nType, int cx, int cy)
{
	// TODO: 在此处添加消息处理程序代码、	CWnd* pWndMapCtrl = GetDlgItem(IDC_MAPCONTROL1);
	CWnd* pWndMapCtrl = GetDlgItem(IDC_MAPCONTROL1);
	if (pWndMapCtrl)
	{
		pWndMapCtrl->MoveWindow(0, 0, cx, cy);
		//pWndMapCtrl->MoveWindow(cx/4, 0, cx*3/4, cy);
	}

	CFormView::OnSize(nType, cx, cy);
}

void CheckerView::AddLayer(ILayerPtr pLayer)
{
	m_ipMapControl->AddLayer(pLayer, 0);
}

void CheckerView::RefreshMap()
{
	m_ipMapControl->Refresh(esriViewAll);
}

ILayerPtr CheckerView::GetLayer(CString strName)
{
	HRESULT hr;
	ILayerPtr pLayer = NULL;
	long numLayers = 0;
	hr = m_ipMapControl->get_LayerCount(&numLayers);
	if (SUCCEEDED(hr))
	{
		for (long i = 0; i < numLayers; i++)
		{
			hr = m_ipMapControl->get_Layer(i, &pLayer);
			if (SUCCEEDED(hr))
			{
				BSTR bstrName;
				hr = pLayer->get_Name(&bstrName);
				if (SUCCEEDED(hr))
				{
					char* szName = _com_util::ConvertBSTRToString(bstrName);
					if (strName == szName)
					{
						delete[] szName;
						break;
					}
					delete[] szName;
				}
			}
		}
	}

	return pLayer;
}

void CheckerView::OnBtnTestLandsat5Metadata()
{
	{	//LandCover30数据读取测试
		const char* szPath = "E:\\Document\\2-项目实施\\TJ-数据质量检查-2018\\样例数据\\30米地表覆盖数据\\龙江局30米地表覆盖";

		RESULTCODE rc = RC_SUCCESS;
		ck::product::LandCover30 lc30;
		lc30.SetPath(szPath);
		rc = lc30.Open();

		ck::EnumString* pDocuments = NULL;
		ck::data::Dataset* pDataset = NULL;
		ck::data::EnumDataset* pDatasets = NULL;
		ck::data::Workspace* pWorkspace = NULL;
		ck::data::EnumWorkspace* pWorkspaces = NULL;
		pWorkspaces = lc30.GetWorkspaces();
		pWorkspaces->Reset();
		while ((pWorkspace = pWorkspaces->Next()))
		{
			TRACE1("[Name]:%s\n", pWorkspace->GetName());

			TRACE1("\t[影像]:%s\n", pWorkspace->GetName());
			TRACE0("\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			pDatasets = pWorkspace->GetDatasets();
			pDatasets->Reset();
			while ((pDataset = pDatasets->Next()))
			{
				TRACE1("\t[Name]:%s\n", pDataset->GetName());
			}
			pDatasets->Release();

			TRACE1("\t[文档]:%s\n", pWorkspace->GetName());
			TRACE0("\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			const char* szDocName = NULL;
			pDocuments = pWorkspace->GetDocuments();
			pDocuments->Reset();
			while ((szDocName = pDocuments->Next()))
			{
				TRACE1("\t[Name]:%s\n", szDocName);
			}
			pDocuments->Release();
		}
		pWorkspaces->Release();
		lc30.Close();
	}

	// TODO: 在此添加命令处理程序代码
	//ck::data::LandsatSchema* pSchema = NULL;
	//ck::data::LandsatSchemaReader reader;

	//const char* szPath = "E:\\Project\\Apps\\zChecker\\Dist\\conf\\Landsat5MetaConstraint.xml";
	//pSchema = reader.Reader(szPath);
	{	//LandSat5元数据检查测试
		ck::data::Landsat05 ls05;
		//ls05.SetPath("E:\\Document\\2-项目实施\\TJ-数据质量检查-2018\\LT05_L1TP_132016_20000320_20161214_01_T1");
		ls05.SetPath("E:\\Document\\2-项目实施\\TJ-数据质量检查-2018\\样例数据\\RAW\\LT05_L1TP_132016_20000320_20161214_01_T1");
		ls05.Open();
		//pSchema = ls05.GetSchema();

		//ck::data::LandsatMeta* pMeta = ls05.GetMetaData();
		ck::data::LandsatRawMeta* pRawMeta = ls05.GetRawMetaData();
		ck::data::Schema* pSchema = ls05.GetSchema();
		{
			ck::check::CheckResult* rst = NULL;
			ck::check::CheckResultSet reset;

			ck::check::LandsatMetaIntegrityChecker intChecker;
			intChecker.SetDataset(&ls05);
			intChecker.Check();
			rst = intChecker.GetResult();
			if (rst)	reset.Add(rst);

			ck::check::LandsatMetaCorrectnessChecker crtChecker;
			crtChecker.SetDataset(&ls05);
			crtChecker.Check();
			rst = crtChecker.GetResult();
			if (rst)	reset.Add(rst);

			ck::check::LandsatFileChecker fileChecker;
			fileChecker.SetDataset(&ls05);
			fileChecker.Check();
			rst = fileChecker.GetResult();
			if (rst)	reset.Add(rst);

			CMainFrame* pFrmMain = (CMainFrame*)AfxGetMainWnd();
			pFrmMain->m_wndOutput.PopulateCheckResult(&reset);
		}
	}
	
	
	//ck::data::LandsatMetaReader reader;
	//ck::data::LandsatRawMeta* pRawMeta = NULL;
	//pRawMeta = reader.ReadRaw("E:\\Document\\2-项目实施\\TJ-数据质量检查-2018\\样例数据\\RAW\\LT05_L1TP_132016_20000320_20161214_01_T1\\LT05_L1TP_132016_20000320_20161214_01_T1_MTL.txt");
	//{
	//	TRACE0("##############################################################\n");
	//	z_uint32 nCount = pRawMeta->GetCount();
	//	ck::data::LandsatRawMetaGroup* pRawGroup = NULL;
	//	for (z_uint32 i = 0; i < nCount; i++)
	//	{
	//		pRawGroup = pRawMeta->GetGroup(i);
	//		if (pRawGroup)
	//		{
	//			TRACE0("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	//			TRACE1("[Group] = %s\n", pRawGroup->GetName());

	//			const char* szKey = NULL;
	//			const char* szText = NULL;
	//			pRawGroup->Reset();
	//			while (!pRawGroup->IsEOF())
	//			{
	//				szKey = pRawGroup->GetKey();
	//				szText = pRawGroup->GetValue();
	//				TRACE2("\t[%s] = %s\n", szKey, szText);
	//				pRawGroup->Next();
	//			}
	//		}
	//	}
	//}
	//pRawMeta->Release();

	AfxMessageBox("OnBtnTestLandsat5Metadata");
}

/************************************************************************/
/* 元数据检查                                                            */
/************************************************************************/
void CheckerView::OnBtnTestLandsatMetadataCheck()
{
	// TODO: 在此添加命令处理程序代码

	ck::data::LandsatProperty prop;

	ck::data::LandsatNameParser parser;
	parser.Parse("LT05_L1TP_132016_20000320_20161214_01_T1", &prop);

	AfxMessageBox("Landsat Meta Data");
}


void CheckerView::OnBtnMetaSchemaLandsat05()
{
	// TODO: 在此添加命令处理程序代码
}


void CheckerView::OnBtnMetaSchemaLandcover30Image()
{
	// TODO: 在此添加命令处理程序代码
	CMetaSchemaDialog dlg;
	dlg.DoModal();
}


void CheckerView::OnBtnMetaSchemaLandcover30Map()
{
	// TODO: 在此添加命令处理程序代码
}
